package com.incross.dawin.http.parser;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.incross.dawin.util.BKLog;

import android.util.Log;

public class BasicInfoParser {
	
	private String mAdSystem = "";
	private String mAdId = "";
	private String mAdTitle = "";
	private String mDescription = "";
	private String mErrorUrl = "";
	private String mAdDuration = "";
	private String mVideoClickThrough = "";
	private String mVideoClickTracking = "";
	// 외부 브라우저 분기부분 추가
	private String mBrowserMode = "";
	
	// 20141216 파싱시 발생한 문제 체크용 boolean값 추가
	private boolean mIsParsingProblemOccured = false;
	
	public BasicInfoParser(Element xmlObject) {
		if(xmlObject != null) {
			try {
				mAdId = xmlObject.getElementsByTagName("Ad").item(0).getAttributes().item(0).getNodeValue();
				// 브라우저 모드의 기본값은 클릭뷰어(앱내부 웹뷰)로 한다.
				mBrowserMode = "0";
				if(mAdId == null || mAdId.equals("")) {
					mAdId = "-1";
				}
				if(mAdId.equals("-1")) {
					BKLog.w("Not applicable ad");
				} else {
					// extract basic info
					NodeList adSystem = xmlObject.getElementsByTagName("AdSystem");
					NodeList adTitle = xmlObject.getElementsByTagName("AdTitle");
					NodeList description = xmlObject.getElementsByTagName("Description");
					NodeList error = xmlObject.getElementsByTagName("Error");
					NodeList adDuration = xmlObject.getElementsByTagName("Duration");
					NodeList videoClick = xmlObject.getElementsByTagName("VideoClicks").item(0).getChildNodes();
					// 외부브라우저 분기부분 추가, 
					NodeList browserMode = xmlObject.getElementsByTagName("BrowserMode"); 
					
					for(int i = 0; i < adSystem.getLength(); i++) {
						if(adSystem.item(i).getFirstChild() != null) {
							mAdSystem = adSystem.item(i).getFirstChild().getNodeValue(); 
						}
					}
					
					for(int i = 0; i < adTitle.getLength(); i++) {
						if(adTitle.item(i).getFirstChild() != null) {
							mAdTitle = adTitle.item(i).getFirstChild().getNodeValue();
						}
					}
					
					for(int i = 0; i < description.getLength(); i++) {
						if(description.item(i).getFirstChild() != null) {
							mDescription = description.item(i).getFirstChild().getNodeValue(); 
						}
					}
					
					for(int i = 0; i < error.getLength(); i++) {
						if(error.item(i).getFirstChild() != null) {
							mErrorUrl = error.item(i).getFirstChild().getNodeValue(); 
						}
					}
					
					for(int i = 0; i < adDuration.getLength(); i++) {
						if(adDuration.item(i).getFirstChild() != null) {
							mAdDuration = adDuration.item(i).getFirstChild().getNodeValue(); 
						}
					}
					
					for(int i = 0; i < videoClick.getLength(); i++) {
						if(videoClick.item(i).getFirstChild() != null) {
							String attrName = videoClick.item(i).getNodeName();
							String value = videoClick.item(i).getFirstChild().getNodeValue();
							
							if(attrName.equals("ClickThrough")) {
								mVideoClickThrough = value;
							} else if(attrName.equals("ClickTracking")) {
								mVideoClickTracking = value;
							}
							
						}
					}
					
					// 브라우저 분기부분 추가
					for(int i = 0; i < browserMode.getLength(); i++) {
						if(adDuration.item(i).getFirstChild() != null) {
							mBrowserMode = browserMode.item(i).getFirstChild().getNodeValue();
							BKLog.d("##### Browser Mode : " + mBrowserMode);
						}
					}
				}
			} catch(Exception e) {
				e.printStackTrace();
				mIsParsingProblemOccured = true;
			}
		} else {
			mIsParsingProblemOccured = true;
		}
	}
	
	// 20141216 파싱시 문제가 발생한 경우 체크를 위한 메소드 추가
	public boolean isParsingProblemOccured() {
		return mIsParsingProblemOccured;
	}
	
	public String getAdId() {
		return mAdId;
	}
	
	public String getAdSystem() {
		return mAdSystem;
	}
	
	public String getAdTitle() {
		return mAdTitle;
	}
	
	public String getAdDescription() {
		return mDescription;
	}
	
	public String getErrorUrl() {
		return mErrorUrl;
	}
	
	public String getAdDuration() {
		return mAdDuration;
	}
	
	public String getVideoClickThrough() {
		return mVideoClickThrough;
	}
	
	public String getVideoClickTracking() {
		return mVideoClickTracking;
	}
	
	// 브라우저 분기 추가
	public String getBrowserMode() {
		return mBrowserMode;
	}

}
